package pet.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pet.po.User;
import pet.service.UserService;
/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String username,String password, Model model,HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(username, password);
		if(user != null){		
			// 将用户对象添加到Session
			int user_id = user.getUser_id();
			int user_ident = user.getIdent();
			session.setAttribute("USER_ID", user_id);
			session.setAttribute("USER_IDENT", user_ident);
			session.setAttribute("USER_SESSION", user);
			if (user.getIdent() == 0)
				// 跳转到管理员主页面
				return "redirect:admin_index.action";
			if (user.getIdent() == 1)
				// 跳转到商户主页面
				return "redirect:business_index.action";
			if (user.getIdent() == 2)
				// 跳转到顾客主页面
				return "redirect:customer_index.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
         // 返回到登录页面
		return "login";
	}
	
	/**
	 * 向用户登录页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
		return "login";
	}
	
	/**
	 * 向用户注册页面跳转
	 */
	@RequestMapping(value = "/register.action", method = RequestMethod.GET)
	public String toRegister() {
		return "register";
	}

	@RequestMapping(value = "/register.action", method = RequestMethod.POST)
	public String register(User user, Model model) {
		int rows=userService.register(user);
	    if(rows > 0){	
	    	System.out.println("注册成功");
	    	return "login";
	    }else{
	    	model.addAttribute("msg", "注册失败，请重新注册！");
			return "register";		
	    }
		
	}
	
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
		// 清除Session
		session.invalidate();
		// 重定向到登录页面的跳转方法
		return "redirect:login.action";
	}
	
	
	/**
	 * 向管理员首页跳转
	 */
	@RequestMapping(value = "/admin_index.action", method = RequestMethod.GET)
	public String getAdmonIndex(Model model) {
		return "admin_index";
	}
	/**
	 * 在管理员首页向顾客管理页面跳转
	 */
	@RequestMapping(value = "/adminc.action", method = RequestMethod.GET)
	public String getUser(Model model) {
		List<User> users=userService.getUser();
		model.addAttribute("users", users);
		return "adminc";
	}
	/**
	 * 管理员删除用户信息
	 */
	@RequestMapping("/deleteUser.action")
	public String deleteUser(int user_id) {
	    int rows = userService.deleteUser(user_id);
	    if(rows > 0){			
	    	System.out.println("删除成功");
	    }else{
	    	System.out.println("删除失败");
	      		
	    }
	    return "admin_index";
	}

	/**
	 * 向顾客首页跳转
	 */
	@RequestMapping(value = "/customer_index.action", method = RequestMethod.GET)
	public String getCustIndex(Model model) {
		return "customer_index";
	}
	
	/**
	 * 向商家首页跳转
	 */
	@RequestMapping(value ="/business_index.action", method = RequestMethod.GET)
	public String getBusinessIndex(Model model) {
		return "business_list";
	}
}

 